package com.project.controller;

import com.project.entity.RestPOJO;
import com.project.entity.Vehicle;
import com.project.service.VehicleService;
import com.project.tools.OSSTools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@Api(tags="用户车辆管理相关")
public class VehicleController {

    @Autowired
    private VehicleService vehicleService;

    @ApiOperation(
            value = "根据用户ID查询用户的所有车辆信息",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class,
            notes = "查询用户的所有车辆信息"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="path",name="id",value="用户的用户id",required=true)
    })
    @GetMapping("/vehicle/{id}")
    public RestPOJO getVehicleByUid(@PathVariable("id") Integer uid){
        List<Vehicle> list = vehicleService.getVehicleByUid(uid);
        RestPOJO restPOJO = new RestPOJO();
        if (list.size() > 0){
            restPOJO.setStatus(1000);
            restPOJO.setMessages("查询成功");
            restPOJO.setData(list);
        }
        else {
            restPOJO.setStatus(1001);
            restPOJO.setMessages("查询失败");
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "增加用户的车辆信息",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class,
            notes = "增加用户车辆"
    )
    @PostMapping("/vehicle")
    public RestPOJO addVehicle(@RequestBody Vehicle vehicle){
        int a = vehicleService.addVehicle(vehicle);
        RestPOJO rest = new RestPOJO();
        if (a > 0){
            rest.setStatus(1000);
            rest.setMessages("增加成功");
        }
        else {
            rest.setStatus(1001);
            rest.setMessages("增加失败");
        }
        return rest;
    }

    @ApiOperation(
            value = "根据车辆ID查询用户的一辆车的信息",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class,
            notes = "查询用户的一辆车的信息"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="path",name="id",value="用户的车辆id",required=true)
    })
    @PostMapping("/vehicle/{id}")
    public RestPOJO getVehicleByid(@PathVariable Integer id){
        Vehicle vehicle = vehicleService.getVehicleById(id);
        RestPOJO retp = new RestPOJO();
        if (vehicle != null){
            retp.setStatus(1000);
            retp.setMessages("查询成功");
            retp.setData(vehicle);
        }
        else {
            retp.setStatus(1001);
            retp.setMessages("查询失败");
        }
        return retp;
    }

    @ApiOperation(
            value = "修改用户的车辆信息",
            httpMethod = "PUT",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class,
            notes = "修改用户车辆"
    )
    @PutMapping("/vehicle")
    public RestPOJO updateVehicle(@RequestBody Vehicle vehicle){
        int a = vehicleService.updateVehicle(vehicle);
        RestPOJO rest = new RestPOJO();
        if (a > 0){
            rest.setStatus(1000);
            rest.setMessages("修改成功");
        }
        else {
            rest.setStatus(1001);
            rest.setMessages("修改失败");
        }
        return rest;
    }

    @ApiOperation(
            value = "修改用户的车辆图片",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class,
            notes = "修改用户车辆图片"
    )
    @PostMapping("/vehicle/photo")
    public RestPOJO updVehiclePhoto(Vehicle vehicle, MultipartFile vehiclePhoto){
        String photo= OSSTools.upLoad(System.currentTimeMillis()+"",vehiclePhoto);
        vehicle.setVehicle_photo(photo);
        int a = vehicleService.updateVehiclePhoto(vehicle);
        RestPOJO rest = new RestPOJO();
        if (a > 0){
            rest.setStatus(1000);
            rest.setMessages("图片修改成功");
        }
        else {
            rest.setStatus(1001);
            rest.setMessages("图片修改失败");
        }
        return rest;
    }

    @ApiOperation(
            value = "删除用户的多个车辆信息",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class,
            notes = "删除用户多个车辆"
    )
    @PostMapping("/vehicle/del")
    public RestPOJO delAllVehicle(@RequestBody Object[]ids){
        RestPOJO rdt = new RestPOJO();
        int a = vehicleService.delVehicleByIds(ids);
        if(a > 0){
            rdt.setMessages("删除成功");
            rdt.setStatus(1000);
        }
        else{
            rdt.setMessages("删除失败");
            rdt.setStatus(1001);
        }
        return rdt;
    }

    @ApiOperation(
            value = "根据用户车辆ID删除用户的一个车辆信息",
            httpMethod = "DELETE",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class,
            notes = "删除用户一个车辆"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="path",name="id",value="用户的车辆id",required=true)
    })
    @DeleteMapping("/vehicle/{id}")
    public RestPOJO delVehicle(@PathVariable("id") Integer id){
        RestPOJO restPOJO = new RestPOJO();
        int a = vehicleService.delVehicleByVid(id);
        if(a > 0){
            restPOJO.setMessages("删除成功");
            restPOJO.setStatus(1000);
        }
        else{
            restPOJO.setMessages("删除失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }
}
